<?php
//Create by Nguyen 23/08/2012
include_once 'YahooOauth.php';
require_once('../../../../../config.php');

$cuncumer_key='dj0yJmk9cW1oSmJ3QVBKU0lKJmQ9WVdrOU1GVkxSVFZ6TldjbWNHbzlORE15TVRFNE1qWXkmcz1jb25zdW1lcnNlY3JldCZ4PWYw';
$cunsumer_secret='cc85b2a791174fce2d39d1a176b61da29c7a95c1';
$argarray = $argv[0];
$debug = 0; // Set to 1 for verbose debugging output
$callback='http://99xu.vn/catalog/controller/account/plugin/Yahoo/GetContact.php';

//oauth_token=kke8tht&oauth_verifier=cxectp

$oauth =new YahooOauth($cuncumer_key, $cunsumer_secret, $argarray, $debug, $callback);

$getcontact_access=new YahooGetContacts();

session_start();
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Get the access token using HTTP GET and HMAC-SHA1 signature

$request_token=$oauth->rfc3986_decode($_GET['oauth_token']);
//echo "Request token :".$request_token;
//echo "<br />";

//$request_token='k57356v';
$request_token_secret=$oauth->rfc3986_decode($_SESSION['oauth_token_secret']);
//echo "Request token secret :".$request_token_secret;
//echo "<br />";

//$oauth_verifier= 'pkzkns';
$oauth_verifier= $oauth->rfc3986_decode($_GET['oauth_verifier']);
//echo "Request token verifier :".$oauth_verifier;
//echo "<br />";

//echo "access token info<br />";


$retarr = $getcontact_access->get_access_token($oauth,
                           $request_token, $request_token_secret,
                           $oauth_verifier, false, true, true);

////////////////////////////////////////////////////////////////////
// Call Contact API

//echo "<br />";


$access_token=$oauth->rfc3986_decode($retarr['oauth_token']);
//echo "Access token :".$access_token;
//echo "<br />";

$access_token_secret=$oauth->rfc3986_decode($retarr['oauth_token_secret']);
//echo "access token sectet :".$access_token_secret;
//echo "<br />";

$guid=$oauth->rfc3986_decode($retarr['xoauth_yahoo_guid']);


$contacts	=	$getcontact_access->callcontact($oauth, $guid, $access_token, $access_token_secret, false, true);

$person=$contacts["contacts"]["contact"];

/*
foreach($person as $value)
{
	$info=$value["fields"][0]["value"];
	if(!strpos($info,"@")) $info=$info . "@yahoo.com";
	echo $info ."<br />";
}
*/

//var_dump($contacts["contacts"]["contact"][1]["fields"][0]["value"]);

/*
//Email Contacts
//Begin get email customer
$demo  = $contacts[0];
$first = current($contacts[0]);
foreach($first as $emailcustomer)
{
	$getmail=$emailcustomer;
}
$first=40;
$last=strpos($getmail,"%40");
$sub=substr($getmail,$first,$last-$first);
$fullemail= $sub . "@gmail.com";
echo "Email customer: ". $fullemail ."<br />";
//End get email customer
*/
$link = mysql_connect(DB_HOSTNAME,DB_USERNAME , DB_PASSWORD);
if(!$link) die('Not connected : ' . mysql_error());
$db_selected = mysql_select_db(DB_DATABASE, $link);
if (!$db_selected) die('Can\'t select : ' . mysql_error());
$select=mysql_query("SELECT * FROM email_banned_emails ORDER BY banid DESC LIMIT 1");
while($row = mysql_fetch_array($select)) 
	{
		$getid=$row['banid'];
		$mainemail=$row['emailaddress'];
	}
$compare=mysql_query("SELECT name FROM email_lists WHERE name='$mainemail'");
mysql_query("DELETE FROM email_banned_emails WHERE banid='$getid'");
if(!mysql_num_rows($compare)) mysql_query("INSERT INTO email_lists (name,format,notifyowner,visiblefields,ownerid) VALUES ('$mainemail','b','1','emailaddress','1')");
$listid=mysql_query("SELECT listid FROM email_lists WHERE name='$mainemail'");
if (mysql_num_rows($listid))
while($row = mysql_fetch_array($listid)) $id=$row['listid'];
if(!empty($person))
{
foreach($person as $value)
{
	$info=$value["fields"][0]["value"];
	if(!strpos($info,"@")) $info=$info . "@yahoo.com";
	mysql_query("INSERT INTO email_list_subscribers (listid,emailaddress,format,confirmed,confirmcode) VALUES ('$id','$info','h','1','9a69e28c1a48e5c35ca9c0865ee0034f')");
}
}
$sql_total = mysql_query("SELECT listid FROM email_lists");
while($row_total = mysql_fetch_array($sql_total)){
    $sql1_total = mysql_query("SELECT emailaddress FROM email_list_subscribers WHERE listid='$row_total[0]'");
    $numRows_total1 = mysql_num_rows($sql1_total);
	$sql2_total = mysql_query("UPDATE email_lists SET subscribecount=$numRows_total1, agreedelete=$numRows_total1  WHERE listid='$row_total[0]'");
 }
mysql_close($link);
header('Location:'.HTTP_SERVER. 'index.php?route=account/account');
?>
	